﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace GameName1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager _graphics;//_graphics: Handles the configuration and management of the graphics device.
        SpriteBatch _spriteBatch;//_spriteBatch: Enables a group of sprites to be drawn using the same settings.
        Camera _gameCamera;//_gameCamera: Camera class.
        Map _gameMap;//_gameMap: Map class.
        Player _gamePlayer;//_gamePlayer: Player class.
        KeyboardState _currentKeyboardState;//__currentKeyboardState: Used to determine if the button is being pressed.
        Keys _lastPressedKey;

        public Game1() : base()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            _gameMap = new Map();
            _gameMap.SetTileDimensions(32, 32);
            _gameMap.GenerateMap();
            _gamePlayer = new Player(32.0f);
            _gamePlayer.SetScreenAndPlayerLimits(
                GraphicsDevice.Viewport.Width,
                GraphicsDevice.Viewport.Height,
                _gameMap.TileCountX * _gameMap.TileWidth,
                _gameMap.TileCountY * _gameMap.TileHeight);
            _gamePlayer.SetStartingPosition(32.0f);
            _gameCamera = new Camera(32.0f);
            _gameCamera.SetScreenAndCameraLimits(
                GraphicsDevice.Viewport.Width, 
                GraphicsDevice.Viewport.Height,
                _gameMap.TileCountX * _gameMap.TileWidth,
                _gameMap.TileCountY * _gameMap.TileHeight);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _gameMap.AddTileTextures(Content, "Graphics\\ground_grass_2", "Graphics\\ground_dirt_1", "Graphics\\test_block_1");
            _gamePlayer.LoadTexture(Content, "Graphics\\test_player_1");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        } 

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            _currentKeyboardState = Keyboard.GetState();
            if (!(_currentKeyboardState.GetPressedKeys().Length > 0))
            {
                _lastPressedKey = Keys.None;
                return;
            }    
            if (_currentKeyboardState.IsKeyDown(Keys.Escape))
                Exit();
            if (_currentKeyboardState.IsKeyDown(_lastPressedKey))
                return;
            _gameCamera.UpdateCamera(_currentKeyboardState, _gamePlayer.Position);
            _gamePlayer.UpdatePlayer();
            _lastPressedKey = _currentKeyboardState.GetPressedKeys()[0];
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            _spriteBatch.Begin();
            _gameMap.DrawMap(_spriteBatch, _gameCamera.Position);
            _gamePlayer.DrawPlayer(_spriteBatch);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}